import { Column, Entity, OneToMany } from 'typeorm';
import { BaseEntity } from '../../../common/entities/base.entity';
import { UserSocialAuth } from './user-social-auth.entity';
import { Comment } from '../../novel/entities/comment.entity';
import { UserFavorite } from '../../novel/entities/user-favorite.entity';
import { UserReadingHistory } from '../../novel/entities/user-reading-history.entity';
import { Bookshelf } from '../../novel/entities/bookshelf.entity';

export enum UserGender {
  UNKNOWN = 0,
  MALE = 1,
  FEMALE = 2,
}

export enum UserStatus {
  DISABLED = 0,
  ACTIVE = 1,
}

@Entity('users')
export class User extends BaseEntity {
  @Column({ type: 'varchar', length: 50, unique: true, comment: '用户名' })
  username: string;

  @Column({ type: 'varchar', length: 100, nullable: true, comment: '昵称' })
  nickname: string;

  @Column({ type: 'varchar', length: 100, nullable: true, comment: '邮箱' })
  email: string | null;

  @Column({ type: 'varchar', length: 20, nullable: true, comment: '手机号' })
  phone: string | null;

  @Column({ type: 'varchar', length: 255, comment: '密码哈希' })
  passwordHash: string;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '头像URL' })
  avatar: string | null;

  @Column({
    type: 'tinyint',
    default: UserGender.UNKNOWN,
    comment: '性别: 0-未知 1-男 2-女',
  })
  gender: UserGender;

  @Column({
    type: 'tinyint',
    default: UserStatus.ACTIVE,
    comment: '状态: 0-禁用 1-正常',
  })
  status: UserStatus;

  @Column({ type: 'datetime', nullable: true, comment: '最后登录时间' })
  lastLoginTime: Date;

  // 关联关系
  @OneToMany(() => UserSocialAuth, (socialAuth) => socialAuth.user)
  socialAuths: UserSocialAuth[];

  @OneToMany(() => Comment, (comment) => comment.user)
  comments: Comment[];

  @OneToMany(() => UserFavorite, (favorite) => favorite.user)
  favorites: UserFavorite[];

  @OneToMany(() => UserReadingHistory, (history) => history.user)
  readingHistory: UserReadingHistory[];

  @OneToMany(() => Bookshelf, (bookshelf) => bookshelf.user)
  bookshelves: Bookshelf[];
}
